﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoftwareConsulting.BI11.Studio.Core.Project;
using SoftwareConsulting.BI11.Studio.Core.Project.Metadata;
using SoftwareConsulting.BI11.Studio.Core.Project.SituationsHierarchy;
using SoftwareConsulting.Interfaces.BasicEntities;
using SoftwareConsulting.BI11.Studio.InternalServices;

namespace SoftwareConsulting.BI11.Studio.UI.ProjectExplorer.SituationsHierarchy.RulesContainers
{
    /// <summary>
    /// Диалог фиксации элементов измерений куба
    /// </summary>
    public partial class FixDimensionItemsDialog : RestorableForm
    {
        private ProjectDescriptor _projectLink; //для связи с проектом
        private FixedDimensionItemsPointer _FixedDimensionItems; //зафиксированные элементы измерений
        private ICube _cube;//редактируемый куб

        public FixedDimensionItemsPointer FixedDimensionItems
        {
            get
            {
                return _FixedDimensionItems;
            }
        }

        /// <summary>
        /// Инициализация диалога
        /// </summary>
        private void Initialize()
        {
            _cube = FixedDimensionItems.OwningObject as ICube;
            Text = Text + " '" + _cube.Name + "'";
            DataGridViewRowCollection rows = FixedDimensionItemsGrid.Rows;
            rows.Clear();
            ICalendarDimension cdim = _cube.GetCalendarDimension();
            if (cdim != null)
            {
                string dimlabel = cdim.Label;                
                rows.Add();
                rows[rows.Count - 1].Cells[0].Value = dimlabel;
                DataGridViewComboBoxCell combo = rows[rows.Count - 1].Cells[1] as DataGridViewComboBoxCell;
                DataGridViewComboBoxCell.ObjectCollection items = combo.Items;
                INamedHierarchy<INamed> ind = FixedDimensionItems.GetFixedDimensionItemByDimension(cdim);
                string defval = ind.NameWithDeep;
                ind = cdim.Items.GetItemByPosition(0, true) as INamedHierarchy<INamed>;
                while (ind != null)
                {
                    items.Add(ind.NameWithDeep);
                    ind = ind.NextSiblingItem as INamedHierarchy<INamed>;
                }
                combo.Value = defval;                
            }
            int c = _cube.GetFactsDimensionsCount();
            for (int i = 0; i < c; i++)            
            {                
                IFactsDimension dim = _cube.GetFactsDimensionByPosition(i);
                if (dim.Items.ItemsCount > 0)
                {
                    rows.Add();
                    rows[rows.Count - 1].Cells[0].Value = dim.Label;
                    DataGridViewComboBoxCell combo = rows[rows.Count - 1].Cells[1] as DataGridViewComboBoxCell;
                    DataGridViewComboBoxCell.ObjectCollection items = combo.Items;
                    INamedHierarchy<INamed> ind = FixedDimensionItems.GetFixedDimensionItemByDimension(dim);
                    string defval = ind.NameWithDeep;
                    ind = dim.Items.GetItemByPosition(0, true) as INamedHierarchy<INamed>;
                    while (ind != null)
                    {
                        items.Add(ind.NameWithDeep);
                        ind = ind.NextSiblingItem as INamedHierarchy<INamed>;
                    }
                    combo.Value = defval;
                }
            }
        }

        /// <summary>
        /// Применяет изменения и закрывает диалог
        /// </summary>
        private void ApplyAndClose()
        {            
            DataGridViewRowCollection rows = FixedDimensionItemsGrid.Rows;
            int c = rows.Count;
            List<DimensionItem> dimitems = new List<DimensionItem>();
            for (int i = 0; i < c; i++)
            {
                string dimlabel = rows[i].Cells[0].Value as string;
                IDimension dim = _projectLink.RepositoryLink.GetObjectDescriptorByLabel(dimlabel, MetadataObjectKind.Dimension) as IDimension;                
                DataGridViewComboBoxCell combo = rows[i].Cells[1] as DataGridViewComboBoxCell;
                DataGridViewComboBoxCell.ObjectCollection items = combo.Items;
                dimitems.Add(new DimensionItem(dim.Label, dim.Items.GetItemByPosition(items.IndexOf(combo.Value as string), true).Tag));
            }
            _FixedDimensionItems = new FixedDimensionItemsPointer(_projectLink, FixedDimensionItems.CharacteristicId, dimitems);
            Close();
        }

        /// <summary>
        /// Конструктор формы
        /// </summary>        
        public FixDimensionItemsDialog(ProjectDescriptor currentProject, FixedDimensionItemsPointer fixedDimensionItems)
        {
            _projectLink = currentProject;
            _FixedDimensionItems = fixedDimensionItems;
            InitializeComponent();
        }

        private void FixDimensionItemsDialog_Load(object sender, EventArgs e)
        {
            Initialize();
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            ApplyAndClose();
        }
    }
}
